package cn.iocoder.yudao.module.product.service.calculationmodel;

import cn.iocoder.yudao.module.product.controller.admin.calculationmodel.vo.ProcessModelCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.calculationmodel.vo.ProcessModelRespVO;
import cn.iocoder.yudao.module.product.controller.admin.calculationmodel.vo.ProcessModelUpdateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.process.vo.EmissionProcessResp1VO;
import cn.iocoder.yudao.module.product.controller.admin.process.vo.EmissionProcessRespVO;
import cn.iocoder.yudao.module.product.controller.admin.process.vo.ProcessPMResVO;
import cn.iocoder.yudao.module.product.convert.process.ProcessConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.calculationmodel.CalculationModelDO;
import cn.iocoder.yudao.module.product.dal.dataobject.process.ProcessDO;
import cn.iocoder.yudao.module.product.dal.dataobject.productmanagement.ProductManagementDO;
import cn.iocoder.yudao.module.product.dal.mysql.calculationmodel.CalculationModelMapper;
import cn.iocoder.yudao.module.product.dal.mysql.process.ProcessMapper;
import cn.iocoder.yudao.module.product.dal.mysql.productmanagement.ProductManagementMapper;
import cn.iocoder.yudao.module.product.service.process.EmissionProcessService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.product.dal.dataobject.calculationmodel.ProcessModelDO;

import cn.iocoder.yudao.module.product.convert.calculationmodel.ProcessModelConvert;
import cn.iocoder.yudao.module.product.dal.mysql.calculationmodel.ProcessModelMapper;

import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.product.enums.ErrorCodeConstants.*;

/**
 * 工序-模型关联 Service 实现类
 *
 * @author wcl
 */
@Service
@Validated
public class ProcessModelServiceImpl implements ProcessModelService {

    @Resource
    private ProductManagementMapper productManagementMapper;

    @Resource
    private ProcessModelMapper processModelMapper;

    @Resource
    private CalculationModelMapper calculationModelMapper;

    @Resource
    private ProcessMapper processMapper;

    @Resource
    private EmissionProcessService emissionProcessService;

    @Override
    public Long createProcessModel(ProcessModelCreateReqVO createReqVO) {
        // 插入
        ProcessModelDO processModel = ProcessModelConvert.INSTANCE.convert(createReqVO);
        processModelMapper.insert(processModel);
        // 返回
        return processModel.getId();
    }

    @Override
    public void updateProcessModel(ProcessModelUpdateReqVO updateReqVO) {
        // 校验存在
        validateProcessModelExists(updateReqVO.getId());
        // 更新
        ProcessModelDO updateObj = ProcessModelConvert.INSTANCE.convert(updateReqVO);
        processModelMapper.updateById(updateObj);
    }

    @Override
    public void deleteProcessModel(Long id) {
        // 校验存在
        validateProcessModelExists(id);
        // 删除
        processModelMapper.deleteById(id);
    }

    private void validateProcessModelExists(Long id) {
        if (processModelMapper.selectById(id) == null) {
            throw exception(PROCESS_MODEL_NOT_EXISTS);
        }
    }

    @Override
    public ProcessModelRespVO getProcessModel(Long id) {
        List<EmissionProcessResp1VO> lists = new ArrayList<>();
        // 查模型
        CalculationModelDO calculationModelDO = calculationModelMapper.selectById(id);
        // 查产品
        ProductManagementDO productManagementDO = productManagementMapper.selectById(calculationModelDO.getProductId());
        // 查模型关联表
        QueryWrapper<ProcessModelDO> queryWrapper0 = new QueryWrapper<>();
        List<ProcessModelDO> processModelDOList = processModelMapper.selectList(queryWrapper0.eq("model_id", id));
        // 查找所有工序
        processModelDOList.forEach(item ->{
            QueryWrapper<ProcessDO> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.lambda().eq(ProcessDO::getId, item.getProcessId());
            ProcessDO processDO = processMapper.selectOne(queryWrapper1);
            // 修改工序信息，新增两个属性
            ProcessPMResVO processPMResVO = ProcessConvert.INSTANCE.convertPM(processDO);
            processPMResVO.setPeriodName(item.getPeriodName());
            processPMResVO.setPmID(item.getId());
            // 为了获取与该工序关联的排放源
            EmissionProcessRespVO emissionProcessRespVO = emissionProcessService.getEmissionProcess(processDO.getId());
            // 组合上述的两个信息到EmissionProcessResp1VO对象中
            EmissionProcessResp1VO emissionProcessResp1VO = new EmissionProcessResp1VO();
            emissionProcessResp1VO.setProcessPMResVO(processPMResVO);
            emissionProcessResp1VO.setEmissions(emissionProcessRespVO.getEmissions());
            lists.add(emissionProcessResp1VO);
            }
        );
        ProcessModelRespVO processModelRespVO = new ProcessModelRespVO();
        processModelRespVO.setCalculationModelDO(calculationModelDO);
        processModelRespVO.setProductManagementDO(productManagementDO);
        processModelRespVO.setLists(lists);
        return processModelRespVO;
    }


}
